package com.ffyc.resale.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.dao.GoodsDao;
import com.ffyc.resale.dao.UserDao;
import com.ffyc.resale.model.Goods;
import com.ffyc.resale.util.JWTUtil;
import com.ffyc.resale.model.GoodsSearch;
import com.ffyc.resale.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class GoodsService {
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    UserDao userDao;
    //查询所有
    public IPage<Goods> list(Goods goods){
        Page<Goods> page = new Page<>(goods.getCurrentPage(),goods.getPageSize());
        IPage<Goods> list = goodsDao.list(page);
        return list;
    }
    //分类查询
    public IPage<Goods> search(Goods form){
        Page<Goods> page = new Page<>(form.getCurrentPage(),form.getPageSize());
        IPage<Goods> goodsIPage ;
        if(!form.getName().equals("")){
            //通过名字
            goodsIPage = goodsDao.searchByName(page, form.getName());
        }else {
            if (form.getStatus()==null){
                //通过类型
                if (form.getGoodType().getTypeId()==null){
                    goodsIPage = goodsDao.searchByType1(page,form.getGoodType().getId());
                }else {
                    goodsIPage = goodsDao.searchByType2(page,form.getGoodType().getTypeId());
                }
            }else {
                if(form.getGoodType().getId()==null){
                    //通过状态
                    goodsIPage = goodsDao.searchByStatus(page,form.getStatus());
                }else {
                    //通过类型和状态
                    if (form.getGoodType().getTypeId()==null){
                        goodsIPage = goodsDao.searchByType1AndStatus(page,form.getGoodType().getId(),form.getStatus());
                    }else {
                        goodsIPage = goodsDao.searchByType2AndStatus(page,form.getGoodType().getTypeId(),form.getStatus());
                    }
                }
            }
        }
        return goodsIPage;
    }
    public void approve(Integer id){
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(1);
        goods.setTime(new Date());
        goodsDao.updateById(goods);
    }
    public void reject(Integer id){
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(2);
        goodsDao.updateById(goods);
    }

    public Page<Goods> myGoods(String userToken, Goods goods) {
        Integer userid = JWTUtil.id(userToken);
        Page<Goods> page = new Page<>(goods.getCurrentPage(),goods.getPageSize());
        Integer status = goods.getStatus();
        Page<Goods> list;
        if(status!=null){
            list = goodsDao.selectByUserIdAndStatus(page,userid,status);
        }else {
            list = goodsDao.selectByUserId(page, userid);
        }
        return list;
    }

    public void delist(Integer id) {
        Goods goods = new Goods();
        goods.setId(id);
        goods.setStatus(3);
        goodsDao.updateById(goods);
    }

    public void update(Goods goods) {
        goods.setStatus(0);
        goodsDao.updateById(goods);
    }
    public void addGoods(String userToken, Goods goods) {
        int id = JWTUtil.id(userToken);
        goods.setUserid(id);
        goods.setStatus(0);
        goodsDao.insert(goods);
    }

    public Page<Goods> findGoods(GoodsSearch goods) {
        Page<Goods> goodsIPage = new Page<>(goods.getCurrentPage(),goods.getPageSize());
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>(goods);
        if(goods.getKeyword()!=null){
            queryWrapper.like("name", goods.getKeyword());
        }
        if (goods.getMaxPrice()!=null){
            queryWrapper.lt("price", goods.getMaxPrice());
        }
        if (goods.getMinPrice()!=null){
            queryWrapper.gt("price", goods.getMinPrice());
        }
        queryWrapper.eq("status", 1);
        queryWrapper.gt("num", 0);
        Page<Goods> page = goodsDao.selectPage(goodsIPage, queryWrapper);
        return page;
    }

    public Goods findGoodsById(Integer id) {
        Goods goods = goodsDao.selectById(id);
        int userId  = goods.getUserid();
        User user = userDao.selectById(userId);
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.eq("userid", user.getId());
        List<Goods> goods1 = goodsDao.selectList(goodsQueryWrapper);
        user.setList(goods1);
        goods.setUser(user);
        return goods;
    }

    public Page<Goods> findGoodsByType(GoodsSearch goodsSearch) {
        Page<Goods> page = new Page<>(1,8);
        QueryWrapper<Goods> goodsSearchQueryWrapper = new QueryWrapper<>();
        goodsSearchQueryWrapper.eq("typeid", goodsSearch.getTypeid()).or().eq("typesid", goodsSearch.getTypesid());
        Page<Goods> page1 = goodsDao.selectPage(page, goodsSearchQueryWrapper);
        return page1;
    }

    public void delete(Integer id) {
        Goods goods = new Goods();
        goods.setId(id);
        goodsDao.deleteById(goods);
    }

    public List<Goods> selectNewGoods() {

        List<Goods> list = goodsDao.selectNewGoods();
        return list;
    }
}
